import os
import yaml

class YamlUtil:
    """
        处理extract.yml文件内容
        function:   read_yaml(读)
                    write_yaml(写)
                    clear_yaml(清除)
        files:      api_yaml, yaml_name接口yaml文档
                    extract_yaml，存放token值yaml文档

    """
    def read_extract_yaml(self, key):
        with open(os.getcwd().split('test_case')[0]+r'\data\extract.yml', mode='r', encoding="utf-8") as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value[key]

    def write_extract_yaml(self, data):
        with open(os.getcwd().split('test_case')[0]+r'\data\extract.yml', 'a', encoding="utf-8") as f:
            yaml.dump(data=data, stream=f, allow_unicode=True)

    def clear_extract_yaml(self):
        with open(os.getcwd().split('test_case')[0]+r'\data\extract.yml', 'w', encoding="utf-8") as f:
            f.truncate()

    def read_api_yaml(self, yaml_name):
        with open(os.getcwd().split('test_case')[0]+'/data/'+yaml_name, mode='r', encoding="utf-8") as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value

if __name__ == '__main__':
    print(os.path.dirname(__file__))

